//
//  BCUserCenterHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/14.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCUserCenterHeaderView.h"

#import "BCUserCenterResponse.h"

/**
 弧形背景layer的高度
 */
static CGFloat BCUserCenterHeaderViewLayerHeight = 96.0f;
/**
 name与label的padding
 */
static CGFloat BCUserCenterHeaderViewNameLabelPadding = 5.0f;
/**
 底部的间隔
 */
static CGFloat BCUserCenterHeaderViewBottomPadding = 20.0f;



//
@implementation BCUserCenterHeaderImageButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.headerIV = [UIImageView new];
        [self addSubview:self.headerIV];
        [self.headerIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        self.adjustsButtonWhenHighlighted = false;
        //
        self.tintButton = [QMUIButton new];
        [self addSubview:self.tintButton];
        [self.tintButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        self.tintButton.highlightedBackgroundColor = RGBHEXA(0x000000, 0.2);
        self.tintButton.userInteractionEnabled = false;
        
        self.layer.cornerRadius = [self.class defaultSize].width/2;
        self.layer.masksToBounds = true;
        self.layer.borderColor = [UIColor qmui_colorWithHexString:@"#FFFFFF"].CGColor;
        self.layer.borderWidth = 2;
        self.backgroundColor = [UIColor qmui_colorWithHexString:@"#D8D8D8"];
    }
    return self;
}
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    self.tintButton.highlighted = highlighted;
}


+ (CGSize)defaultSize {
    return CGSizeMake(90.0, 90.0);
}

@end

/**
 底部数据视图
 */
#import "BCUserCenterCoinDataView.h"

@interface BCUserCenterHeaderView () <BCUserCenterCoinDataViewDelegate>

/**
 弧形layer
 */
@property (nonatomic, strong) CAShapeLayer *arcLayer;

/**
 *stretch拉伸视图 contentOffset.y <0 时拉伸
 */
@property (nonatomic, strong) UIView *stretchView;

/**
 *BCUserCenterHeaderImageButton
 */
@property (nonatomic, strong) BCUserCenterHeaderImageButton *headerImageButton;

/**
 *nameButton
 */
@property (nonatomic, strong) QMUIButton *nameButton;

/**
 *uidLabel
 */
@property (nonatomic, strong) QMUILabel *uidCodeLabel;

/**
 *recommendCodeLabel
 */
//@property (nonatomic, strong) QMUILabel *recommendCodeLabel;


#pragma mark - bottom

/**
 *bottomLine
 */
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation BCUserCenterHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
        //
        self.bottomLine = [UIView new];
        self.bottomLine.backgroundColor = [UIColor separatorColor];
        [self addSubview:self.bottomLine];
        [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1.0);
            make.centerX.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
            make.width.mas_equalTo(self).offset(-20.0);
        }];
    }
    return self;
}

- (void)setupSubviews {
    self.arcLayer = [CAShapeLayer layer];
    self.arcLayer.fillColor = [UIColor darkMainColor].CGColor;
    self.arcLayer.strokeColor = self.arcLayer.fillColor;
    [self.layer addSublayer:self.arcLayer];
    //
    UIBezierPath *arcLayerPath = [UIBezierPath bezierPath];
    [arcLayerPath moveToPoint:CGPointZero];
    [arcLayerPath addLineToPoint:CGPointMake(0, BCUserCenterHeaderViewLayerHeight)];
    [arcLayerPath addQuadCurveToPoint:CGPointMake(self.frame.size.width, BCUserCenterHeaderViewLayerHeight) controlPoint:CGPointMake(self.frame.size.width/2, BCUserCenterHeaderViewLayerHeight/2)];
    [arcLayerPath addLineToPoint:CGPointMake(self.frame.size.width, 0)];
    [arcLayerPath closePath];
    self.arcLayer.path = arcLayerPath.CGPath;
    //
    self.stretchView = [UIView new];
    self.stretchView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.stretchView];
    [self.stretchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_top);
        make.height.mas_equalTo(CGFLOAT_MAX);
    }];
    //
    CGSize headerImageButtonSize = [BCUserCenterHeaderImageButton defaultSize];
    self.headerImageButton = [BCUserCenterHeaderImageButton new];
    [self addSubview:self.headerImageButton];
    [self.headerImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(headerImageButtonSize);
        make.centerY.mas_equalTo(self.mas_top).offset(BCUserCenterHeaderViewLayerHeight*0.75);
    }];
    [self.headerImageButton addTarget:self action:@selector(clickedToUserInfoAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.nameButton = [QMUIButton new];
    [self.nameButton setTitleColor:[UIColor qmui_colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    self.nameButton.titleLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightMedium];
    self.nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self addSubview:self.nameButton];
    [self.nameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.headerImageButton.mas_bottom).offset(7.0);
//        make.width.mas_equalTo(self).offset(-20.0);
        make.height.mas_equalTo(self.nameButton.titleLabel.font.lineHeight+5);
    }];
    [self.nameButton addTarget:self action:@selector(clickedToUserInfoAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.uidCodeLabel = [QMUILabel new];
    self.uidCodeLabel.font = [self.class labelFont];
    self.uidCodeLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.uidCodeLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.uidCodeLabel];
    [self.uidCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.nameButton.mas_bottom).offset(BCUserCenterHeaderViewNameLabelPadding);
        make.width.mas_lessThanOrEqualTo(self).offset(-20.0);
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.arcLayer.frame = self.bounds;
}

/**
 tableView的contentOffset发生变化
 */
- (void)contentOffsetChanged:(CGPoint)contentOffset {
    //更新拉伸视图的高度约束
//    CGFloat stretchHeight = MAX(0, -contentOffset.y);
//    [self.stretchView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(stretchHeight);
//    }];
}

#pragma mark - otherAction
//去用户个人信息的一些action
- (void)clickedToUserInfoAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userCenterHeaderViewClickedToUserInfoAction)]) {
        [self.delegate userCenterHeaderViewClickedToUserInfoAction];
    }
}

//重新加载数据
- (void)reloadData {
    if ([BCUserManager sharedUser].isLogin) {
        UserInfoModel *userInfo = [BCUserManager sharedUser].userInfo;
        //
        [self.headerImageButton.headerIV sd_setImageWithURL:[NSURL URLWithString:userInfo.head_img] placeholderImage:nil];
        //
        [self.nameButton setTitle:userInfo.nickname forState:UIControlStateNormal];
    }else {
        self.headerImageButton.headerIV.image = LOAD_IMAGE(@"default_head");
        [self.nameButton setTitle:self.centerData.flow.not_login forState:UIControlStateNormal];
    }
    [self setUIDCode];
}
//
- (void)setUIDCode {
    if (self.centerData.user) {
        NSString *uid = [@"UID:" stringByAppendingString:self.centerData.user.user_id];
        NSString *code = [NSString stringWithFormat:@"%@:%@",self.centerData.flow.share_code,self.centerData.user.share_code];
        //
        self.uidCodeLabel.text = [NSString stringWithFormat:@"%@    %@",uid,code];
    }else {
        self.uidCodeLabel.text = nil;
    }
}


//
- (void)setCenterData:(BCUserCenterData *)centerData {
    _centerData = centerData;
    //
    [self reloadData];
}



+ (UIFont *)labelFont {
    return [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
}
+ (CGFloat)labelHeight {
    return [self labelFont].lineHeight+1;
}

+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    /**
     头像和弧形谁高选谁
     */
    height += MAX(BCUserCenterHeaderViewLayerHeight, BCUserCenterHeaderViewLayerHeight*.75+[BCUserCenterHeaderImageButton defaultSize].height/2);
    //
    height += ([UIFont systemFontOfSize:23 weight:UIFontWeightMedium].lineHeight+12);//name高度和间隔
    //
    height += (BCUserCenterHeaderViewNameLabelPadding+[self labelHeight]);
    //
    height += BCUserCenterHeaderViewBottomPadding;
    return height;
}

@end
